<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .active{
            background-color: rgb(99, 35, 35);
            color: #fff;
        }
    </style>
    <!-- 1.引入vue.js -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
    <!-- 2.将vue实例渲染到DOM节点上 -->
     
    <div id="app">
        <ul>
            <!-- 单选: :class="{active:idIndex == index}" -->
            <!-- 多选: :class="{active:item.bon} -->
            <li :class="{active:item.bon}" v-for="(item,index) in list" :key="index" @click="doclick(index)">{{ item.name }}</li>
        </ul>
    </div>

    <script>
        // 3.new一个vue实例
        let app = new Vue({
            el: '#app',
            data: {
                list:[
                    {name:'首页',bon:false},
                    {name:'娱乐',bon:false},
                    {name:'体育',bon:false},
                ],
                idIndex:''
            },
            methods:{
                doclick(index){
                // 单选:将选中的下标赋值给一个变量,在class变量处进行判断,相等就添加
                //   this.idIndex = index
                
                // 多选:将class的变量设置为布尔值,然后利用取反操控布尔值
                 this.list[index].bon = !this.list[index].bon
                }
            }
        })
    </script>

</body>

</html>